<?php
/**
 * V7 checked 1
 *
 */
class data_feed extends data {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 当public时才发布feed
	 *
	 * @param int $article_id
	 * @return boll
	 */
	public function add_by_event_on_save_article ($article_id) {
		$article_id = intval($article_id);
		$data_save_article = new data_save_article();
		$is_public = $data_save_article->is_public($article_id);
		
		if ($article_id and $is_public) {
			return $this->db->execute('
				insert ignore into
					feed
				(sender_id, receiver_id, type, source_id, dt)
					select
						save_article.user_id,
						friend.friend_id, 
						"save_article",
						save_article.id,
						save_article.dt
					from
						save_article, friend
					where
						save_article.id		= "'.$article_id.'"			and
						friend.user_id		= save_article.user_id
			');
		} else return false;
	}
	/**
	 * 列出feed
	 *
	 * @param int $uid
	 * @param int $page		// 最小为1
	 * @param int $limit		// 最小为1
	 * @return array
	 */
	public function ls ($uid, $page=1, $limit=10) {
		$uid		= intval($uid);
		$page	= ($page > 1) ? intval($page) : 1;
		$limit		= ($limit > 1) ? intval($limit) : 1;

		if ($uid) {
			$execute = $this->db->execute('
				select
					feed.id			AS id,
					feed.sender_id		AS sender_id,
					feed.receiver_id		AS receiver_id,
					feed.type			AS type,
					feed.source_id		AS source_id,
					feed.dt			AS dt,
					IF (feed.type = "save_article", 
						(select title from save_article where id=feed.source_id),
						(select title from circle where id=feed.source_id)
					)				AS title
				from
					feed
				where
					feed.receiver_id 		= "'.$uid.'"
				order by
					id DESC
				limit
					'.(($page-1)*$limit).', '.$limit.'
			');
			
			if (is_array($execute)) {
				$data_user = new data_user();
				foreach ($execute as $k=>$v) {
					$execute[$k]['friend_nickname'] = $data_user->friend_to_nickname($uid, $v['sender_id']);			// 转换好友昵称
				}
				return $execute;
			} else return false;
		} else return false;
	}
	public function event_del_by_save_article ($article_ids_str) {
		return $this->db->execute('
			delete from
				feed
			where
				type		= "save_article"	and
				source_id	IN ('.$article_ids_str.')
		');
	}
	public function event_add_by_circle_action ($uid, $circle_id, $type) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		$type		= ($type === 'create_circle' or $type === 'join_circle' or $type === 'follow_circle') ? $type : false;
		
		if ($uid and $circle_id and $type) {
			// 检查circle是否是public
			$this->db->execute('
				insert ignore into
					feed
				(sender_id, receiver_id, type, source_id, dt)
					select
						"'.$uid.'",
						friend.friend_id,
						"'.$type.'",
						"'.$circle_id.'",
						"'.date('Y-m-d H:i:s').'"
					from
						circle, friend
					where
						circle.id		= "'.$circle_id.'"	and
						circle.privacy	= "public"		and
						friend.user_id	= "'.$uid.'"
			');
			return true;
		} else return false;
	}
	/**
	 * 读出feed中的一条article记录
	 * 		返回$data_save_article one的结果
	 *
	 * @param int $uid			// 不能为0
	 * @param int $feed_id			// feed表中的主键id
	 * @return array
	 */
	public function one_article ($uid, $feed_id) {
		$uid		= intval($uid);
		$feed_id	= intval($feed_id);
		
		if ($uid and $feed_id) {
			// 在publish feed的时候，我们只发布有阅读权限的feed，所以此处不用再次验证
			$feed = $this->db->execute('
				select
					feed.id			AS id,
					save_article.id		AS article_id,
					save_article.id		AS content_id,
					save_article.user_id	AS user_id,
					save_article.title		AS title,
					save_article.dt		AS dt,
					article_url.id		AS url_id,
					article_url.url		AS url
				from
					feed, save_article, article_url
				where
					feed.id		= "'.$feed_id.'"		and
					feed.receiver_id	= "'.$uid.'"			and
					feed.type 		= "save_article"		and
					save_article.id	= feed.source_id		and
					article_url.id	= save_article.url_id
			');
			if ($feed) {
				return $feed[0];
			} else return false;
		} else return false;
	}
	public function event_copy_feed ($sender_id, $receiver_id) {
		$sender_id		= intval($sender_id);
		$receiver_id		= intval($receiver_id);
		
		if ($sender_id and $receiver_id) {
			$this->db->execute('
				insert ignore into
					feed
				(sender_id, receiver_id, type, source_id, dt)
					select
						"'.$sender_id.'",
						"'.$receiver_id.'",
						type,
						source_id,
						dt
					from
						feed
					where
						sender_id	= "'.$sender_id.'"
					group by
						source_id, type
					order by
						id ASC
			');
			// 交换位置，再来一次
			$this->db->execute('
				insert ignore into
					feed
				(sender_id, receiver_id, type, source_id, dt)
					select
						"'.$receiver_id.'",
						"'.$sender_id.'",
						type,
						source_id,
						dt
					from
						feed
					where
						sender_id	= "'.$receiver_id.'"
					group by
						source_id, type
					order by
						id ASC
			');
			return true;
		} else return false;
	}
}
?>